package dao;

import pojo.Members;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminSql {
    public static Members adminLogin(String adminname)  {
        String sql="select * from members where name='"+adminname+"'";
        ResultSet rs = JDBCconection.getData(sql);
        Members members=null;
        try {
            while (rs.next()){
                members=new Members();
                members.setId(rs.getInt(1));
                members.setName(rs.getString(2));
                members.setSex(rs.getString(3));
                members.setPwd(rs.getString(4));
                members.setTel(rs.getString(5));
                members.setHeadPortrait(rs.getString(6));
            }
        }catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return members;
    }
    //利用管理员的名字和性别查询管理员信息
    public static List<Members> getMemberName(String name, String sex){
        List<Members> list=new ArrayList<>();
        String sql="select name,sex from members where name like '%"+name+"%' and sex like '%"+sex+"%'";
        ResultSet  rs= JDBCconection.getData(sql);
        Members m=null;
        try {
            while(rs.next()){
                m=new Members();
                m.setName(rs.getString(1));
                m.setSex(rs.getString(2));
                list.add(m);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }
    //注册管理员用户
    public static Boolean addMembers(Members m) {
        String sql="insert into members values(null,'"+m.getName()+"','"+m.getSex()+"','"+m.getPwd()+"','"+m.getTel()+"','"+m.getHeadPortrait()+"')";
        boolean updata = JDBCconection.updata(sql);
        return updata;
    }

    public static List<Members> queryMember(String name) {
        List<Members> list=new ArrayList<>();
        String sql="select * from members where name='"+name+"'";
        ResultSet  rs= JDBCconection.getData(sql);
        Members m=null;
        try {
            while(rs.next()){
                m=new Members();
                m.setId(rs.getInt(1));
                m.setName(rs.getString(2));
                m.setSex(rs.getString(3));
                m.setPwd(rs.getString(4));
                m.setTel(rs.getString(5));
                list.add(m);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }

    public static Boolean updateMember(Members m) {
        String sql="update members set name='"+m.getName()+"',sex='"+m.getSex()+"',tel='"+m.getTel()+"' where id='"+m.getId()+"'";
        return JDBCconection.updata(sql);
    }

    public static String getOldpwd(int id) {
        String pwd=null;
        String sql="select pwd from members where id='"+id+"'";
        ResultSet  rs= JDBCconection.getData(sql);
        try {
            while(rs.next()){
                pwd=rs.getString(1);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return pwd;
    }
    public static boolean  updatePwd(int id,String pwd){
        String sql="update members set pwd='"+pwd+"' where id='"+id+"'";
        return JDBCconection.updata(sql);
    }
    //查询当前管理员信息
    public static Members queryAdmin(String name) throws SQLException {
        String sql="select * from members where name='"+name+"'";
        ResultSet rs = JDBCconection.getData(sql);
        Members members=null;
        while (rs.next()){
            members=new Members();
            members.setId(rs.getInt(1));
            members.setName(rs.getString(2));
            members.setSex(rs.getString(3));
            members.setPwd(rs.getString(4));
            members.setTel(rs.getString(5));
            members.setHeadPortrait(rs.getString(6));
        }
        return members;
    }
}
